package com.axter.chrome.ui;

import com.axter.chrome.Main;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.DragEvent;
import com.axter.chrome.bookmark.BookmarkAnalysis;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

public class BookmarkController {
    private final String title = "选择收藏夹HTML文件...";
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextField tf_ago;

    @FXML
    private Button btn_new;

    @FXML
    private Button btn_repeat;

    @FXML
    private Button btn_change;

    @FXML
    private Button btn_export;

    @FXML
    private Button btn_comp;

    @FXML
    private Button btn_ago;

    @FXML
    private TextArea ta_console;

    @FXML
    private TextField tf_new;

    @FXML
    void drop_ago(DragEvent event) {
        SomeEvent.DragDrop(event, tf_ago);
    }

    @FXML
    void over_ago(DragEvent event) {
        SomeEvent.DragOver(event, "html");
    }

    @FXML
    void open_ago(ActionEvent event) {
        SomeEvent.openFile(Main.stage, tf_ago, title, "html");
    }

    @FXML
    void drop_new(DragEvent event) {
        SomeEvent.DragDrop(event, tf_new);
    }

    @FXML
    void over_new(DragEvent event) {
        SomeEvent.DragOver(event, "html");
    }

    @FXML
    void open_new(ActionEvent event) {
        SomeEvent.openFile(Main.stage, tf_new, title, "html");
    }

    @FXML
    void action_repeat(ActionEvent event) {
        if (checkFile(tf_ago.getText())) {
            ta_console.setText(BookmarkAnalysis.checkRepeatUrls(tf_ago.getText()));
        }
    }

    @FXML
    void action_comp(ActionEvent event) {
        if (checkFile(tf_ago.getText()) && checkFile(tf_new.getText())) {
            ta_console.setText(BookmarkAnalysis.compareLeftAndRight(tf_ago.getText(), tf_new.getText()));
        }
    }

    @FXML
    void action_change(ActionEvent event) {
        String ago = tf_ago.getText();
        String n = tf_new.getText();
        tf_ago.setText(n);
        tf_new.setText(ago);
    }

    @FXML
    void action_export(ActionEvent event) {
        if (checkFile(tf_ago.getText())) {
            ta_console.setText(BookmarkAnalysis.exportBookmarkToHtmlFile(tf_ago.getText()));
        }
    }

    public static boolean checkFile(String path) {
        File file = new File(path);
        if (file.exists() && file.isFile()) {
            return true;
        } else {
            SomeEvent.createAlert(Main.stage, "请输入待检测文件");
        }
        return false;
    }
}

